package com.monkey.web.context.strategy;

import com.monkey.core.domain.BaseEntity;
import com.monkey.core.web.response.MonkeyResponseDto;
import com.monkey.utils.MonkeyUtils;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * 返回值是MonkeyResponseDto
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/9/1
 */
public class MonkeyResponseDtoCtrlReturnValueHandler extends AbstractCtrlReturnValueHandler {

    @Override
    public boolean support(Object returnValue) {
        return returnValue instanceof MonkeyResponseDto;
    }

    @Override
    public MonkeyResponseDto handle(Object returnValue, String fieldStr) {
        MonkeyResponseDto responseDto = (MonkeyResponseDto) returnValue;
        Object data = responseDto.getData();
        if (MonkeyUtils.isEmpty(data)) {
            return responseDto;
        }
        if (data instanceof BaseEntity) {
            responseDto.setData(this.exportResultData(data, fieldStr));
        } else if (data instanceof List) {
            responseDto.setData(this.exportResultList(fieldStr, (List) data));
        } else if (data instanceof Page) {
            responseDto.setData(this.exportResultPage(fieldStr, (Page) data));
        }
        return responseDto;
    }
}
